<template>
  <!-- 新增类型dialog -->
  <Edialog
    :title="title"
    :outershow="show"
    saveClick
    @close="closeFn"
    @saveFn="saveFn"
  >
    <div style="padding: 0 20px">
      <el-form ref="form" :model="form" label-width="80px" :rules="leftRules">
        <el-form-item prop="groupName" label="分组名称">
          <el-input v-model="form.groupName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.groupNotes" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
    </div>
  </Edialog>
</template>

<script>
import Edialog from '@/components/Edialog'
import {
  putBsContactsGroup,
  postBsContactsGroup
} from '@/api/smsManage/groupContacts'
import { fd } from '@/utils/index'
export default {
  components: {
    Edialog
  },
  data () {
    return {
      title: '',
      show: false,
      form: {
        groupName: '',
        groupNotes: '',
        id: ''
      },
      leftRules: {
        groupName: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  methods: {
    open (scope) {
      if (!scope) {
        this.title = '新增'
      } else {
        this.title = '修改'
        this.form.groupNotes = scope.groupNotes
        this.form.groupName = scope.groupName
        this.form.id = scope.id
      }
      this.show = true
    },
    // 保存事件
    saveFn () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res
          if (this.title == '新增') {
            res = await putBsContactsGroup(fd(this.form))
          } else {
            res = await postBsContactsGroup(fd(this.form))
          }
          if (res.data && res.data.bcode == 0) {
            this.$parent.getData()
            this.closeFn()
            this.$message.success(res.data.bmsg)
          }
        }
      })
    },
    closeFn () {
      this.form.groupName = ''
      this.form.groupNotes = ''
      this.form.id = ''
      this.$refs.form.clearValidate()
      this.show = false
    }
  }
}
</script>

<style scoped lang="scss"></style>
